﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace GameFrameLibrary
{
    /// <summary>
    /// 动态显示文字的用户控件，超级文本框类型。
    /// </summary>
    public partial class HyperTextBlock : UserControl
    {
        public HyperTextBlock()
        {
            InitializeComponent();
        }

        public new double FontSize { get { return this.myAnimatedTextBlock.FontSize; } set { this.myAnimatedTextBlock.FontSize = value; } }

        public new Brush Foreground { get { return this.myAnimatedTextBlock.Foreground; } set { this.myAnimatedTextBlock.Foreground = value; } }

        public TextWrapping TextWrapping { get { return this.myAnimatedTextBlock.TextWrapping; } set { this.myAnimatedTextBlock.TextWrapping = value; } }

        public TextTrimming TextTrimming { get { return this.myAnimatedTextBlock.TextTrimming; } set { this.myAnimatedTextBlock.TextTrimming = value; } }


        public static readonly DependencyProperty HyperTextProperty =
            DependencyProperty.Register("HyperText", typeof(string), typeof(HyperTextBlock), new FrameworkPropertyMetadata("", OnHyperTextChanged));

        private static void OnHyperTextChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
        {
            ((HyperTextBlock)dp).OnHyperTextChanged(e);
        }

        public string HyperText
        {
            get
            {
                return (string)GetValue(HyperTextProperty);
            }
            set
            {
                SetValue(HyperTextProperty, value);
            }
        }

        private TimeSpan hyperTextInterval = TimeSpan.FromMilliseconds(50);
        public TimeSpan HyperTextInterval
        {
            get { return this.hyperTextInterval; }
            set { this.hyperTextInterval = value; }
        }

        protected virtual void OnHyperTextChanged(DependencyPropertyChangedEventArgs e)
        {
            var Text = e.NewValue as string;

            var storyboard = this.myAnimatedTextBlock.Resources["Animation"] as Storyboard;

            storyboard.Stop();

            var stringAnime = storyboard.Children[0] as StringAnimationUsingKeyFrames;

            stringAnime.KeyFrames.Clear();

            stringAnime.Duration = TimeSpan.FromMilliseconds(Text.Length * this.HyperTextInterval.TotalMilliseconds);

            var beginTime = TimeSpan.FromMilliseconds(0);

            for (int i = 0; i < Text.Length; i++)
            {
                var currentString = "";

                for (int j = 0; j <= i; j++)
                {
                    currentString += Text[j];
                }

                var keyTime = KeyTime.FromTimeSpan(beginTime);

                beginTime += this.HyperTextInterval;

                stringAnime.KeyFrames.Add(new DiscreteStringKeyFrame() { Value = currentString, KeyTime = keyTime });
            }

            storyboard.Begin();
        }
    }
}
